//
//  MyUtils.m
//  CarDeal_IOS
//
//  Created by 孙可 on 2019/2/19.
//  Copyright © 2019年 sunke. All rights reserved.
//

#import "MyUtils.h"
#import "AddCustomViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "DialogShowChooseCheckView.h"
#import "DialogChooseMoreView.h"

@implementation MyUtils


+(void)showChooseView:(UIView *)parentView withTitle:(NSString *)title withPropertyName:(NSString *)propertyName andData:(NSArray *)dataArray withBlock:(void (^)(id))block{
    DialogChooseView * dialog = [[DialogChooseView alloc]initWith:CGSizeMake(kScreen_Width-100, 350) title:title withArray:dataArray andType:1];
    dialog.propertyName = propertyName;
    dialog.frame = CGRectMake(0, kSafeArea_Top, kScreen_Width, kScreen_Height-kSafeArea_Bottom);
    dialog.didSelect = ^(id entity) {
        block(entity);
    };
    [UIView transitionWithView:parentView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
     [parentView addSubview:dialog];
    } completion:nil];
}

+(void)showChooseCheckView:(UIView *)parentView withTitle:(NSString *)title withPropertyName:(NSString *)propertyName andData:(NSArray *)dataArray withBlock:(void (^)(id))block{
    DialogShowChooseCheckView * dialog = [[DialogShowChooseCheckView alloc]initWith:CGSizeMake(kScreen_Width-100, 350) title:title withArray:dataArray andType:1];
    dialog.propertyName = propertyName;
    dialog.frame = CGRectMake(0, kSafeArea_Top, kScreen_Width, kScreen_Height-kSafeArea_Bottom);
    dialog.didSelect = ^(id entity) {
        block(entity);
    };
    [UIView transitionWithView:parentView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        [parentView addSubview:dialog];
    } completion:nil];
}

+(void)showChooseManyView:(UIView *)parentView withTitle:(NSString *)title withPropertyName:(NSString *)propertyName andData:(NSArray *)dataArray withBlock:(void (^)(NSArray * array))block{
    DialogChooseMoreView * dialog = [[DialogChooseMoreView alloc]initWith:CGSizeMake(kScreen_Width-100, 350) title:title withArray:dataArray andType:1];
    dialog.propertyName = propertyName;
    dialog.frame = CGRectMake(0, kSafeArea_Top, kScreen_Width, kScreen_Height-kSafeArea_Bottom);
    dialog.didSelect = ^(NSArray * array) {
        block(array);
    };
    [UIView transitionWithView:parentView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        [parentView addSubview:dialog];
    } completion:nil];
}


+(void)showTipDialog:(NSString *)message withSureHandler:(void (^)(UIAlertAction *))handler withController : (UIViewController *) controller{
    [self showTipDialog:@"温馨提示" withMessage:message withCancelTitle:@"取消" withSureTitle:@"确认" withCancelHandler:nil withSureHandler:handler withController : (UIViewController *) controller];
}

+(void)showTipDialog:(NSString *)message withSureTitle:(NSString *)sureTitle withSureHandler:(void (^)(UIAlertAction *))handler withController : (UIViewController *) controller{
    [self showTipDialog:@"温馨提示" withMessage:message withCancelTitle:@"取消" withSureTitle:sureTitle withCancelHandler:nil withSureHandler:handler withController : (UIViewController *) controller];
}

+(void)showTipDialog:(NSString *)message withCancelHandler:(void (^)(UIAlertAction *))cancelHandler withSureHandler:(void (^)(UIAlertAction *))sureHandler withController : (UIViewController *) controller{
    [self showTipDialog:@"温馨提示" withMessage:message withCancelTitle:@"取消" withSureTitle:@"确认" withCancelHandler:cancelHandler withSureHandler:sureHandler withController : (UIViewController *) controller];
}

+(void)showTipDialog:(NSString *)title withMessage:(NSString *)message withCancelTitle:(NSString *)cancelTitle withSureTitle:(NSString *)sureTitle withCancelHandler:(void (^)(UIAlertAction *))cancelHandler withSureHandler:(void (^)(UIAlertAction *))sureHandler withController : (UIViewController *) controller{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle: UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancelHandler]];
    [alert addAction:[UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDestructive handler:sureHandler]];
    [controller presentViewController:alert animated:YES completion:nil];
}

+(void)addCustomTip : (UIViewController *) controller{
    [self showTipDialog:@"系统中没有该用户,是否去添加" withSureTitle:@"去添加" withSureHandler: ^(UIAlertAction * _Nonnull action) {
        AddCustomViewController * addVC = [[AddCustomViewController alloc]init];
        addVC.isHiddenTitle = NO;
        addVC.title = @"新建客户";
        [controller.navigationController pushViewController:addVC animated:YES];
    }withController:controller];
}

+(void)loadImageWithUrl:(NSString *)url intoView:(UIImageView *)imageView{
    NSString * allUrl = [NSString stringWithFormat:@"%@%@",IMAGE_PATH,url];
    [imageView sd_setImageWithURL:[NSURL URLWithString:allUrl] placeholderImage:[UIImage imageNamed:@"logo.png"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        NSLog(@"imageerror== %@",error);
    }];
    
}


+(void)loadImageWithGGJUrl:(NSString *)url intoView:(UIImageView *)imageView{
    NSString * allUrl = [NSString stringWithFormat:@"%@%@",ggjPath,url];
    [imageView sd_setImageWithURL:[NSURL URLWithString:allUrl] placeholderImage:[UIImage imageNamed:@"logo.png"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        NSLog(@"imageerror== %@",error);
    }];
}


@end
